<template>
  <div class="app-container">
    <el-card class="box-card">
      <div class="flex items-center justify-between">
        <el-tabs v-model="listQuery.level" @tab-click="handleClick">
          <el-tab-pane label="初级宝箱" :name="1"></el-tab-pane>
          <el-tab-pane label="中级宝箱" :name="2"></el-tab-pane>
          <el-tab-pane label="高级宝箱" :name="3"></el-tab-pane>
        </el-tabs>
        <div>
          <span>活动产出：</span>
          <span>{{ statistics }}M豆</span>
        </div>
      </div>

      <div class="flex justify-between mt-4 mb-4">
        <div class="flex justify-end items-center">
          <div class="flex items-center mr-4">
            <span class="item">用户编号：</span>
            <el-input v-model="listQuery.user_code" clearable placeholder="请输入用户编号" size="normal"></el-input>
          </div>
          <div class="flex items-center mr-4">
            <span class="item">奖励类型：</span>
            <el-select v-model="listQuery.type" placeholder="请选择" clearable filterable>
              <el-option v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
            
          </div>
          <div class="flex items-center mr-4">
            <span class="item">中奖时间：</span>
            <el-date-picker
              v-model="listQuery.startTime"
              type="datetime"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="请选择开始时间"
              class="mr-3"
            >
            </el-date-picker>
            <el-date-picker
              v-model="listQuery.endTime"
              type="datetime"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="请选择结束时间"
            >
            </el-date-picker>
          </div>
          <el-button size="default" type="primary" @click="handleSearch">搜索</el-button>
          <el-button size="default" type="primary" @click="handleReset">重置</el-button>
        </div>
      </div>

      <el-table
        ref="multipleTable"
        v-loading="loading"
        :data="tableData"
        border
        class="mt-3"
        stripe
        style="width: 100%"
        tooltip-effect="dark"
      >
        <el-table-column :index="indexMethod" label="序号" type="index" width="120px"></el-table-column>
        <el-table-column label="用户编号" prop="user.user_code"></el-table-column>
        <el-table-column label="用户昵称" prop="user.nickname"></el-table-column>
        <el-table-column label="获得奖励" prop="open_detail[0].desc"></el-table-column>
        <el-table-column label="奖励价值" prop="total_price"></el-table-column>
        <el-table-column label="奖励类型" prop="type">
          <template slot-scope="{row}">
            <el-tag v-if="row.type === 1" type="success" size="normal" effect="dark">普通奖励</el-tag>
            <el-tag v-else size="normal" effect="dark">限定奖励</el-tag>
          </template>
        </el-table-column>
        <!-- <el-table-column label="获得奖励" prop="detail">
          <template slot-scope="{row}">
            <el-popover
              placement="left"
              width="400"
              trigger="hover">
                <div v-for="item in row.log" :key="item.id" class="flex mb-4 items-center">
                  <img :src="item.detail.picture" alt="" style="max-width: 80px;max-height: 80px">
                  <p class="ml-4">
                    {{item.detail.name}}
                  </p>
                  <p v-if="item.count > 1">
                    <span>数量：</span>{{ item.count }}
                  </p>
                  <p v-if="item.reward_type === 'product'">
                    <span>有效期：</span>{{ item.number }}天
                  </p>
                </div>
                <div class="popoverHeight"  slot="reference">
                  <div v-for="(item, index) in row.log" :key="item.id" class="flex mb-4">
                    <p v-if="index<3">
                      {{item.detail.name}}
                    </p>
                    <p v-if="item.count > 1 && index < 3">
                      <span>数量：</span>{{ item.count }}
                    </p>
                    <p v-if="item.reward_type === 'product' && index < 3">
                      <span>有效期：</span>{{ item.number }}天
                    </p>
                  </div>
                </div>
            </el-popover>
          </template>
        </el-table-column> -->
        
        <el-table-column label="中奖时间" prop="created_at"></el-table-column>
      </el-table>

      <!-- 分页 -->
      <cus-pagination
        v-show="total > 0"
        :limit.sync="listQuery.perPage"
        :page.sync="listQuery.page"
        :total="total"
        @pagination="getTableData()"
      />
    </el-card>

  </div>
</template>

<script>
import { 
  rechargeRewardLog,
  rechargeOpenTotal
} from '@/api/dailyFirstRecharge'

export default {
  name: 'exchangeRecord',
  components: {},
  data() {
    return {
      dialogVisible: false,
      dialogVisible2: false,
      loading: false,
      tableData: [],
      listQuery: {
        page: 1,
        perPage: 10,
        level: 1,
        type: undefined, //1:普通奖励配置 2:限定奖励配置
        user_code: undefined,
        startTime: undefined,
        endTime: undefined
      },
      total: 1,
      options: [
        {
          value: 1,
          label: '普通奖励'
        },
        {
          value: 2,
          label: '限定奖励'
        },
      ],
      count: 0,
      price: 0,
      statistics: 0
    }
  },
  created() {
    this.getTableData()
    this.getRechargeOpenTotal()
  },
  methods: {
    indexMethod(index) {
      return this.total - (this.listQuery.page - 1) * this.listQuery.perPage - index
    },
    handleClick(val) {
      this.listQuery.level = val.name
      this.listQuery.user_code = undefined
      this.listQuery.type = undefined
      this.listQuery.startTime = undefined,
      this.listQuery.endTime = undefined
      this.getTableData()
      this.getRechargeOpenTotal()
    },
    // 网络请求
    /**
     * getTableData
     * @desc 获取表格数据
     */
    async getTableData() {
      this.loading = true
      for (let key in this.listQuery) {
        this.listQuery[key] = !this.listQuery[key] ? undefined : this.listQuery[key]
      }
      
      const { data, code } = await rechargeRewardLog(this.listQuery)
      if (code === 200) {

        this.tableData = data.list
        this.total = data.meta.total
      }
      this.loading = false

    },
    getRechargeOpenTotal() {
      rechargeOpenTotal({
        startTime: this.listQuery.startTime,
        endTime: this.listQuery.endTime,
        type: this.listQuery.type,
        level: this.listQuery.level
      }).then(res => {
        if(res.code === 200) {
          this.statistics = res.data.data
        }
      })
    },
    handleSearch() {
      this.listQuery.page = 1
      this.getTableData()
      this.getRechargeOpenTotal()
    },
    handleReset() {
      this.listQuery = {
        page: 1,
        perPage: 10,
        level: 1,
        type: undefined, //1:普通奖励配置 2:限定奖励配置
        user_code: undefined,
        startTime: undefined,
        endTime: undefined
      }
      this.getTableData()
      this.getRechargeOpenTotal()
    },
    
  }
}
</script>

<style lang="scss" scoped>
.infoBox {
  margin-top      : 15px;
  margin-bottom   : 15px;
  padding-bottom  : 15px;
  border          : 1px solid #999999;
  border-radius   : 10px;
  display         : flex;
  align-items     : baseline;
  justify-content : center;
  box-sizing      : border-box;
  padding-right   : 30px;

  .info_1 {
    display         : flex;
    justify-content : center;
    flex-direction  : column;
    width           : 100%;

    .info_2 {
      margin-top  : 15px;
      display     : flex;
      align-items : center;
      position    : relative;

      span {
        width      : 180px;
        text-align : right;
      }

      .tip {
        color     : red;
        font-size : 12px;
        position  : absolute;
        top       : 100%;
        left      : 120px;
      }
    }
  }
}

.item {
  width : 100px;
}

.formItemBox {
  display     : flex;
  align-items : center;
  flex-wrap   : wrap;

  width       : 100%;
  overflow    : hidden;
}

::v-deep {
  .formItemBox {
    .el-form-item {
      width : 50% !important;
    }
  }
  .el-table {
    .cell {
      display: block !important;
    }
  }
}
.imageIcon {
  width: 80px;
}
</style>

